package liuwanghui.service.impl;

import liuwanghui.common.Result;
import liuwanghui.dto.request.UserProfileUpdateDTO;
import liuwanghui.mapper.UserMapper;
import liuwanghui.pojo.User;
import liuwanghui.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Long getCurrentUserId() {
        // TODO: 实现获取当前用户ID的逻辑
        return null;
    }

    @Override
    public Result getProfile() {
        Long userId = getCurrentUserId();
        User user = userMapper.findById(userId);
        if (user == null) {
            return Result.error("用户不存在");
        }
        user.setPassword(null); // 清除敏感信息
        return Result.success(user);
    }

    @Override
    public Result updateProfile(Long userId, UserProfileUpdateDTO dto) {
        userMapper.updateProfile(userId, dto);
        return Result.success("更新成功");
    }

    @Override
    public Result updatePassword(Long userId, String oldPassword, String newPassword) {
        User user = userMapper.findById(userId);
        if (user == null) {
            return Result.error("用户不存在");
        }
        
        // 验证旧密码
        if (!user.getPassword().equals(oldPassword)) {
            return Result.error("原密码错误");
        }
        
        user.setPassword(newPassword);
        userMapper.updatePassword(user);
        return Result.success("密码修改成功");
    }
} 